%% Author: kevin.jingqiu
%% Created: Jun 8, 2009
%% Description: TODO: Add description to p14
-module(p14).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([solve/0, seq/1]).

%%
%% API Functions
%%
solve() -> solve(1, 1000000, 1, 1).

solve(Cur, To, N, Longest) ->
    case Cur < To of
        true ->
            Length = length(seq(Cur)),
            io:format("~p:~p~n", [Cur, Length]),
            case Length > Longest of
                true ->
                    solve(Cur+1, To, Cur, Length);
                _ ->
                    solve(Cur+1, To, N, Longest)
            end;
        _ ->
            {N, Longest}
    end.


seq(N) ->
    seq(N, []).

%%
%% Local Functions
%%
seq(N, Seq) ->
    case N of
        1 -> [1|Seq];
        _ -> seq(func(N), [N|Seq])
    end.


func(N) ->
    case mylib:is_odd(N) of
        true -> 3*N+1;
        false -> N div 2
    end.

